<?php

namespace App\Jobs;



use App\Libraries\Tools\ConstVar;
use App\Models\HongBao\HongBao;
use App\Models\Order\Order;

class ClosePaymentJob extends Job
{


    private $order_id;


    public function __construct($sid)
    {
        $order_info = explode('_', $sid);
        $this->order_id = $order_info[0];
    }

    /**
     * Execute the job.
     *
     * @param Order $order
     * @param HongBao $hongBao
     * @return bool
     * @throws \Exception
     */
    public function handle(Order $order, HongBao $hongBao)
    {
        $data = $order->where('id', $this->order_id)->first();
        if(empty($data)){
            throw new \Exception('fail on order id='.$this->order_id);
        }
        if($data->status == 0){
            if(!empty($data->hong_bao_id)){
                $hongBao->refill($data->hong_bao_id);
            }
            $data->hong_bao_id = 0;
            $data->hong_bao_amount = 0;
            $data->status = ConstVar::$ORDER_STATUS_EXPIRED;
            $data->save();
        }
        return true;
    }
}
